package leetcode100;

import java.util.Arrays;
import java.util.HashMap;

public class lc016 {
    public static void main(String[] args) {
        int[] nums = new int[]{7,6,4,3,1,1,1,1,1,1};
        Arrays.fill(nums,2);
        int res = majorityElement(nums);
        System.out.println("res:"+res);

    }

    public static int majorityElement(int[] nums) {
        /* map < 数字， 出现次数> */
        HashMap<Integer,Integer> map = new HashMap<>();

        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0)+1);
            if (map.get(num) > nums.length/2) return num;
        }
        return nums[0];
    }
}
